from django.contrib import admin
from d115.polls.models import *
from d115.mixins import AuditAdminMixin, PositionAdminMixin

class QuestionOptionInline(admin.TabularInline):
    model = QuestionOption

class QuestionnaireAdmin(admin.ModelAdmin):
    list_display = ('language', 'code_name', 'name', 'is_active')
    list_display_links = ('code_name', 'name')
    list_filter = ('language',)
    search_fields = ['name', 'code_name',]

class QuestionAdmin(PositionAdminMixin):
    list_display = ('questionnaire', 'position', 'question', 'qtype', 'is_active')
    list_display_links = ('question', )
    list_filter = ('questionnaire',)
    inlines = [ QuestionOptionInline, ]
    search_fields = ['question', ]
    ordering = ['questionnaire', 'position']

class AnswerInline(admin.TabularInline):
    model = Answer
    fields = ['question_display', 'answer', 'decoded_answer']
    readonly_fields = ['question_display', 'answer', 'decoded_answer', ]
    can_delete = False
    extra = 0

class AnswerSetAdmin(AuditAdminMixin):
    list_display = ('created', 'created_by', 'full_name', 'city', 'email', 'phone',)
    list_display_links = ('created', 'created_by', 'full_name')
    search_fields = ['full_name', 'city', 'email', 'phone']
    inlines = [ AnswerInline, ]
    readonly_fields = ['poll', 'full_name', 'city', 'email', 'phone', 'for_print']

    def has_add_permission(self, request):
        return False

class AnswerAdmin(admin.ModelAdmin):
    list_display = ('answer_set', 'question', 'answer')
    list_display_links = ('question', 'answer')
    list_filter = ('answer_set', 'question', )

    def has_add_permission(self, request):
        return False

admin.site.register(Questionnaire, QuestionnaireAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(AnswerSet, AnswerSetAdmin)
#admin.site.register(Answer, AnswerAdmin)
